<?php
class Merchant_Model_Alert
{

	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_Alert();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='alert_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}

	public function delete($value,$field='alert_id'){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);
		return $this->_table->delete($where);
	}


	public function getByValue($value,$field='alert_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

	public function getByCond($cond,$type,$order='alert_id desc',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		$select->where(' alert_status != ?',2);
		$select->where(' customer_id = ?',$cond['customer_id']);		
		if(isset($cond['alert_level']) && $cond['alert_level'] !== ''){
			$select->where(' alert_level = ?',$cond['alert_level']);
		}
		if(isset($cond['alert_status']) && $cond['alert_status'] !== ''){
			$select->where(' alert_status = ?',$cond['alert_status']);
		}
		if(isset($cond['alert_ref_id']) && $cond['alert_ref_id'] !=''){
			$select->where(' alert_ref_id = ?',$cond['alert_ref_id']);
		}
		if(isset($cond['dateFrom']) && $cond['dateFrom'] !=''){
			$select->where(' alert_create_time > ?',$cond['dateFrom'].' 00:00:00');
		}
		if(isset($cond['dateTo']) && $cond['dateTo'] !=''){
			$select->where(' alert_create_time < ?',$cond['dateTo'].' 23:59:59');
		}

		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        $select->order($order);
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		
		$select->limit($count,$offset);	
		return $this->_table->fetchAll($select)->toArray();
	}
	
 
}